/*
 *@(#)Subjectimpl.java 1.0 Feb 18, 2008
 *Copyright Fution Bai
 *
 */
package codebush.service.impl;

import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;

import codebush.domain.Article;
import codebush.domain.Comment;
import codebush.service.Get;
import codebush.service.Subject;
import codebush.service.support.mail.CodebushMailSender;
import codebush.util.ContentUtil;

/**
 * 
 * @author Fution Bai
 * @since 1.0
 */
public class SubjectImpl extends SimpleJdbcDaoSupport implements Subject {

	private CodebushMailSender codebushMailSender;
	private Get get;

	/**
	 * getter for codebushMailSender
	 * 
	 * @return
	 */
	public CodebushMailSender getCodebushMailSender() {
		return codebushMailSender;
	}

	/**
	 * setter for codebushMailSender
	 * 
	 * @param codebushMailSender
	 */
	public void setCodebushMailSender(CodebushMailSender codebushMailSender) {
		this.codebushMailSender = codebushMailSender;
	}

	/**
	 * getter for get
	 * 
	 * @return
	 */
	public Get getGet() {
		return get;
	}

	/**
	 * setter for get
	 * 
	 * @param get
	 */
	public void setGet(Get get) {
		this.get = get;
	}

	/**
	 * email a subject that the registered users want to recommend it to his/her
	 * friend
	 */
	public void emailSubject(String from, String to, String subject, String id, String recContent) {
		Article article = this.getGet().getArticle(Long.parseLong(id));
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("article", article);
		model.put("recContent", recContent);
		String[] toAddress = ContentUtil.strWithCommasToStrArray(to);
		this.getCodebushMailSender().send(from, toAddress, subject, this.getCodebushMailSender().getRecommendationOfTopicMailBodyContentTemplateViewName(), model);
	}

	/**
	 * user post a comment to a article
	 */
	public void reply(long aid, String comment, int userId) {
		String sql = "insert into comment(content, subject_id, user_id)values('" + comment + "','" + aid + "','" + userId + "')";
		getJdbcTemplate().execute(sql);
	}

	/**
	 * delete an unwanted comment
	 */
	public void deleteComment(long commentId) {
		String sql = "delete from comment where id='" + commentId + "'";
		getJdbcTemplate().execute(sql);
	}
	
	/**
	 * add a link alias to the article to make a user friendly link
	 */
	public void addLinkAliasToAnArticle(int articleId, String linkAlias) {
		String sql = "update article set link_alias = ? where id =?";
		Object[] args ={linkAlias, articleId};
		getJdbcTemplate().update(sql, args);
	}

}
